package com.example.test.service.impl;

import com.example.test.Dao.BraendDao;
import com.example.test.bean.BraendType;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.mapper.BraendTypeMapper;
import com.example.test.service.BraendTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@Service
public class BraendTypeServiceImpl extends ServiceImpl<BraendTypeMapper, BraendType> implements BraendTypeService {

    @Resource
    private BraendDao braendDao;
    private final Logger LOGGER = LoggerFactory.getLogger(BraendTypeService.class);

    //分页查询
    public R getAllBraend(PageModel pageModel){
        try {
            Long current = pageModel.getCurrent();
            Long size = pageModel.getSize();
            Long offset = (current - 1) * size;
            List<BraendType> braendList = braendDao.getAllBraend(size,offset);
            return R.success(braendList);
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询品牌失败:" + e.getMessage());
        }
    }
    //根据keyword查询
    public R getBraendByKeyword(String keyword, PageModel pageModel){
        try {
            Long current = pageModel.getCurrent();
            Long size = pageModel.getSize();
            Long offset = (current - 1) * size;
            List<BraendType> braendList = braendDao.getByKeyword(keyword,size,offset);
            return R.success(braendList);
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询品牌失败:" + e.getMessage());
        }
    }

    //根据id查询
    public R getBraendById(Long braendId){
        try {
            BraendType braend = braendDao.getById(braendId);
            if(braend != null){
                return R.success(braend);
            }
            return R.error("查询失败");
        } catch (Exception e) {
            LOGGER.error("", e);
            return R.error("查询品牌失败:" + e.getMessage());
        }
    }

    //新增品牌
    public R addBraend(String braendName){
        try {
            BraendType newBraend = new BraendType();
            //查询是否有对应的品牌，如果没有，则新增
            if(braendName != null && braendName.length()>1){
                Long braendId = braendDao.getIdByName(braendName);
                if(braendId == null){
                    newBraend.setDeleted(false);
                    newBraend.setBraendName(braendName);
                    int addBraend = braendDao.insert(newBraend);
                    if(addBraend == 0){
                        return R.error("新建品牌失败");
                    }
                    return R.success("插入成功"+braendName);
                }
                return R.error("已有该品牌");

            }else{return R.error("braendName为空");}
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("新增商品失败:" + e.getMessage());
        }
    }

    //更新品牌
    public R updateBraend(BraendType braendType){
        try {
            Long braendId = braendType.getBraendId();
            String braendName = braendType.getBraendName();
            if(braendId != null && braendName.length()>1){
                //更新数据库
                int updatenum = braendDao.updateById(braendType);
                if(updatenum == 0){
                    return R.error("更新品牌失败：updatenum为0");
                }
                return R.success("更新品牌成功:"+ braendName);
            }else{return R.error("braendId及braendName不能为空");}
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("更新品牌失败:" + e.getMessage());
        }
    }

    //删除品牌
    public R deleteBraend(Long braendId){
        try {
            BraendType braendType = new BraendType();
            //设置braendType
            braendType.setBraendId(braendId);
            braendType.setDeleted(true);
            //更新数据库
            int deletenum = braendDao.updateById(braendType);
            if(deletenum == 0){
                return R.error("删除品牌失败：deletenum为0");
            }
            return R.success("删除品牌成功:"+ braendId);
        }catch(Exception e) {
            LOGGER.error("", e);
            return R.error("删除品牌失败:" + e.getMessage());
        }
    }

}
